<?php
session_start();
include 'nav.inc.php';
include 'dbConn.php';
html_head('Neuen Nutzer anlegen');

echo("<link rel='stylesheet' type='text/css' href='tcal.css' />");
echo("<link rel='stylesheet' type='text/css' href='styleVerteiler.css' media='all' />\n");
echo("<script type='text/javascript' src='tcal.js'></script>\n");
echo "<script type='text/javascript' src='verteiler.js'></script>";

menu();

echo("<div class='content'>\n");


$vorname = "";
$nachname = "";
$comment = "";
$until = "";
$email = "";
$force = 0;
$lists = array();

if (isset($_REQUEST['submitBTN']) || $_REQUEST['force'])
{
	$vorname = $_REQUEST['vorname'];
	$nachname = $_REQUEST['nachname'];
	$email = $_REQUEST['email'];
	$comment = $_REQUEST['content'];
	$until = $_REQUEST['day'];
	$force = $_REQUEST['force'];
	$lists = $_REQUEST['listsToPutIn'];
}

echo "<form id='newMember' name='newMember' action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n";
echo "\t<div id='newuser'><fieldset>
  <legend>Neuen Nutzer anlegen</legend>
  <ol class='clearfix'>
    <li>
      <label for='vorname'>Vorname:</label>
      <input type='text' name='vorname' id='vorname' onfocus='if (document.getElementById(\"submitBTN\").value === \"Eingetragen\") {colorReset(\"submitBTN\"); emptyAll();}' value='".$vorname."' />
    </li>
    <li>
      <label for='nachname'>Nachname:</label>
      <input type='text' name='nachname' id='nachname' onfocus='if (document.getElementById(\"submitBTN\").value === \"Eingetragen\") {colorReset(\"submitBTN\"); emptyAll();}' value='".$nachname."' />
    </li>
    <li>
      <label for='email'>E-Mail:</label>
      <input type='text' name='email' id='email' value='".$email."' />
      <input type='hidden' name='force' id='force' value='0' />
    </li>
    <li>
      <label for='day'>Bis? (falls bekannt):</label>
		<input type='text' name='day' id='day' class='tcal' value='".$until."' />
    </li>
    <li>
      <label for='content'>Kommentar:</label>
		<textarea cols='32' rows='7' name='content' id='content'>".$comment."
		</textarea>
    </li>
    <li>";
			$result = $dbVerteiler->getLists();
			$c = 0;
			while ($row = mysql_fetch_array($result))
			{	
					if ($c == 5) {echo "</li><li><input type='hidden'/>"; $c = 0; } else $c++;
					if (in_array($row['LID'],$lists))
						echo ("<input type='checkbox' checked='checked' id='list_".$row['LID']."'name='listsToPutIn[]' value='".$row['LID']."'>".$row['Titel']."<br>");	
					else
						echo ("<input type='checkbox' id='list_".$row['LID']."'name='listsToPutIn[]' value='".$row['LID']."'>".$row['Titel']."<br>");	
			}

  echo" </li>
    <li class='last'>
      <input type='submit' name='submitBTN' id='submitBTN' value='Eintragen' onclick='this.form.force.value=\"0\"' />
    </li>
  </ol>
  </fieldset></div>";
echo "\t</form>\n";

if (isset($_REQUEST['submitBTN']) || $force == 1)
{
	$ret = $dbVerteiler->insertNewMember($vorname, $nachname, $comment, $until, 
																$email, $force, $lists);
	
	switch ($ret)
	{
		case 2:
			echo "<script language=javascript>		
						var sName=prompt('Achtung: \\n\\nEine Person mit dem Namen \"".$vorname." ".$nachname."\" exisitert bereits in der Datenbank. \\nBitte geben Sie einen eindeutigen Namen an, oder bestätigen sie den Doppeleintrag durch einen Klick auf \'OK\'.', '".$vorname." ".$nachname."');
						var names = sName.split(' ');
						var vorname = names[0];		
						for( var k=1; k<names.length-1; k++ ) {
    						vorname = vorname + ' ' + names[k];
						}
						var nachname = names[k];	
						changeNewUser(vorname,nachname,'".$comment."','".$until."','".$email."','force', '1');
	  				  </script>";
			break;
		case 3:
			$ret = $dbVerteiler->getUserByAdress($email);
			$row = mysql_fetch_array($ret);
			echo "<script language=javascript>
					alert('Diese Adresse wird schon verwendet! \\n".
"(Und zwar von ".$row['Vorname']." ".$row['Nachname'].")');
						changeNewUser('".$vorname."','".$nachname."','".$comment."','".$until."','".$email."','force', '2');
					</script>";
			break;
		case 1:
		default:
			echo 
			"
			<div style='margin-left:220px;'><h3>Nutzer erfolgreich eingetragen!</h3></div>
			<br/>";

			echo "<script language=javascript>colorswitch('submitBTN');</script>";
			break;
	}
}
echo "</div>";
?>
